﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using CocisModel.Common;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.ManageAsset
{
    public partial class AddDBInstance : BasePage
    {
        public AssetDAL dal = new AssetDAL();
        public LogsDAL logs = new LogsDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindDropDownList();
            }
        }

        public void BindDropDownList()
        {

            this.ddlServerName.DataSource = dal.GetServer();
            this.ddlServerName.DataTextField = "ServerName";
            this.ddlServerName.DataValueField = "ServerID";
            this.ddlServerName.DataBind();

            this.ddlServerIP.DataSource = dal.GetServer();
            this.ddlServerIP.DataTextField = "ServerIP";
            this.ddlServerIP.DataValueField = "ServerID";
            this.ddlServerIP.DataBind();

            this.ddlDBType.DataSource = dal.GetDBType();
            this.ddlDBType.DataTextField = "DBType";
            this.ddlDBType.DataValueField = "DBTypeID";
            this.ddlDBType.DataBind();

            this.ddlStatus.DataSource = dal.GetStatus();
            this.ddlStatus.DataTextField = "Status";
            this.ddlStatus.DataValueField = "StatusID";
            this.ddlStatus.DataBind();

            this.ddlClusterName.DataSource = dal.GetClusterServer();
            this.ddlClusterName.DataTextField = "ClusterName";
            this.ddlClusterName.DataValueField = "ClusterID";
            this.ddlClusterName.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Asset.DBInstanceInfoDataTable dt = new Asset.DBInstanceInfoDataTable();
            Asset.DBInstanceInfoRow rows = dt.NewDBInstanceInfoRow();
            Guid gd = new Guid();
            rows.DBInstanceID = gd;

            //当引入同一个ID的两个属性时，该如何写？
            rows.ServerID = new Guid(this.ddlServerName.SelectedValue.ToString());
            rows.ServerID = new Guid(this.ddlServerIP.SelectedValue.ToString());
            rows.FullInstanceName = this.txtFullInstanceName.Text;
            rows.DBTypeID = int.Parse(this.ddlDBType.SelectedValue.ToString());
            rows.DBVersion = this.txtDBVersion.Text;
            rows.DBServicePack = this.txtDBServicePack.Text;
            rows.AuthenticationMode = this.txtAuthenticationMode.Text;
            rows.StatusID = int.Parse(this.ddlStatus.SelectedValue.ToString());
            rows.SQLNetworkName = this.txtSQLNetworkName.Text;
            rows.SQLNetworkIP = this.txtSQLNetworkIP.Text;
            rows.ClusterID = this.ddlClusterName.SelectedValue.ToString();
          
                
            dt.Rows.Add(rows);
            dt.AcceptChanges();

            int temp = dal.AddDBInstance(dt);
            if (temp > 0)
            {
                string ip = Request.UserHostAddress.ToString();
                int t = logs.AddNewLogsInfo("insert new DBInstanceInfo", LoginUser.UserName, ip);
                ClientScript.RegisterStartupScript(this.GetType(), "a", "<script language=javascript>alert('添加成功！')</script>");
               
              

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "b", "<script language=javascript>alert('添加失败！')</script>");
 
            }


        }
       
    }
}
